package com.example.units;

import android.annotation.SuppressLint;
import android.util.Log;

import java.util.HashMap;
import java.util.Map;

public class TeamList {

	private Map<Integer, Team> container;
	private int count;
	private int maxCount = 4;
	
	@SuppressLint("UseSparseArrays")
	public TeamList()
	{
		this.container = new HashMap<Integer, Team>();
		this.count = 0;
	}
	
	public void addTeam(int key, Team team)
	{
		this.container.put(key, team);
	}
	
	public void addTeam(Team team)
	{
		this.container.put(this.container.size() + 1, team);
		this.count = this.count + 1;
	}
	
	public Map<Integer, Team> getAllTeams()
	{
		return container;
	}
	
	public void setTeams(Map<Integer, Team> teams)
	{
		this.container = teams;
	}
	
	public Team getTeamByNumber(int key)
	{
		return this.container.get(key);
	}
	
	public void eraseTeam(int key)
	{
		if (this.container.containsKey(key))
		{
			this.container.remove(key);
		}
	}
	
	public void eraseAllTeams()
	{
		this.container = new HashMap<Integer, Team>();
		this.count = 0;
	}
	
	public int getTeamCount()
	{
		return this.container.size();
	}
	
	public int getNotGamingFirstCommand()
	{
		int i = 0;
		for (i = 0; i<maxCount; i++)
		{
			if (container.containsKey(i))
			{
				if (container.get(i).getGameRound() == false)
					return i;
			}
		}
		return -1;
	}
	
	public void setRoundFalseAll()
	{
		int i = 0;
		for (i = 0; i<maxCount; i++)
		{
			if (container.containsKey(i))
			{
				container.get(i).setGameRound(false);
			}
		}
	}
	
	public int getScore(int numb)
	{
		return container.get(numb).getScore();
	}
	
	public void setCurrent(int numb)
	{
		Team cTeam = container.get(numb);
		cTeam.setGameRound(true);
		container.remove(numb);
		container.put(numb, cTeam);
	}
	
	public void setScoreCurrent(int numb, int score)
	{
		Team cTeam = container.get(numb);
		cTeam.setScore(score);
		container.remove(numb);
		container.put(numb, cTeam);
	}
	
	private int getFirstCommand()
	{
		int i = 0;
		for (i = 0; i<maxCount; i++)
		{
			if (container.containsKey(i))
			{
				return i;
			}
		}
		return -1;
	}
	
	public int getMaximalScore()
	{
		int i = 0;
		int max = 0;
		int first = 0;
		if (container.size() > 0)
		{
			first = getFirstCommand();
			if (first != -1)
			{
				max = container.get(first).getScore();
				for (i = 1; i<maxCount; i++)
				{
					if (container.containsKey(i))
					{
						if (container.get(i).getScore() > max)
							max = container.get(i).getScore();
					}
				}
			}
		}
		return max;
	}
	
}
